/*
 * $Id: UIHelper.java 28 2013-05-21 12:11:22Z antonyhands@gmail.com $
 */
package ru.olympus.antonyhands.ao;

import java.util.List;

import ru.olympus.antonyhands.ao.data.AOData;
import ru.olympus.antonyhands.ao.data.entities.AOItemBase;
import ru.olympus.antonyhands.ao.data.util.EItemParentType;
import ru.olympus.antonyhands.ao.ui.AddEditAOItem;
import ru.olympus.antonyhands.ao.ui.AddEditAOItemExchange;
import ru.olympus.antonyhands.ao.ui.AddItemBase;
import ru.olympus.antonyhands.ao.ui.UpdateMarketPrices;
import ru.olympus.antonyhands.ao.ui.analysys.CheapestWayToGetItem;
import ru.olympus.antonyhands.ao.ui.info.ItemByCategoryInfo;
import ru.olympus.antonyhands.ao.ui.info.ItemByCraftSkillInfo;

import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.Panel;

/**
 * @author AntonyHands
 * @since 30.08.2012
 */
public class UIHelper
{
    private static Panel _mainPanel;

    static
    {
        if (_mainPanel == null)
        {
            _mainPanel = new Panel();
            _mainPanel.addComponent(new com.vaadin.ui.Label("Welcome!"));
        }
    }

    public static void changeCurrentComponent(Component component)
    {
        _mainPanel.removeAllComponents();
        if (component != null)
            _mainPanel.addComponent(component);
        _mainPanel.requestRepaintAll();
    }

    public static void clearScreen()
    {
        _mainPanel.removeAllComponents();
        _mainPanel.requestRepaintAll();
    }

    public static void addStatusLabel(String labelText)
    {
        _mainPanel.addComponent(new Label(labelText));
    }

    public static MenuBar createMainMenu()
    {
        MenuBar menu = new MenuBar();

        MenuItem itemsListMenu = menu.addItem("Список предметов", null);
        itemsListMenu.addItem("По типу предмета", new Command()
        {
            private static final long serialVersionUID = 911724344232839831L;

            @Override
            public void menuSelected(MenuItem selectedItem)
            {
                _mainPanel.removeAllComponents();
                _mainPanel.addComponent(new ItemByCategoryInfo());
            }
        });
        itemsListMenu.addItem("По навыку производства", new Command()
        {
            private static final long serialVersionUID = 7130652020369540654L;

            @Override
            public void menuSelected(MenuItem selectedItem)
            {
                _mainPanel.removeAllComponents();
                _mainPanel.addComponent(new ItemByCraftSkillInfo());
            }
        });

        MenuItem analysysMenu = menu.addItem("Анализ", null);
        analysysMenu.addItem("Уточнить рыночные цены", new Command()
        {
            private static final long serialVersionUID = 7130652020369540654L;

            @Override
            public void menuSelected(MenuItem selectedItem)
            {
                _mainPanel.removeAllComponents();
                _mainPanel.addComponent(new UpdateMarketPrices());
            }
        });
        analysysMenu.addItem("Самый дешёвый способ получить предмет", new Command()
        {
            private static final long serialVersionUID = 7907228496247515732L;

            @Override
            public void menuSelected(MenuItem selectedItem)
            {
                _mainPanel.removeAllComponents();
                _mainPanel.addComponent(new CheapestWayToGetItem());
            }
        });

        MenuItem storageMenu = menu.addItem("Хранилище", null);
        MenuItem addToStorageMenu = storageMenu.addItem("Добавить", null);
        addToStorageMenu.addItem("Категорию", new Command()
        {
            private static final long serialVersionUID = -808873005070539832L;

            @Override
            public void menuSelected(MenuItem selectedItem)
            {
                _mainPanel.removeAllComponents();
                _mainPanel.addComponent(new AddItemBase(EItemParentType.ITEM_CATEGORY));
            }
        });
        addToStorageMenu.addItem("Производственное умение", new Command()
        {
            private static final long serialVersionUID = 7907228496247515732L;

            @Override
            public void menuSelected(MenuItem selectedItem)
            {
                _mainPanel.removeAllComponents();
                _mainPanel.addComponent(new AddItemBase(EItemParentType.CRAFT_SKILL));
            }
        });
        addToStorageMenu.addItem("Предмет", new Command()
        {
            private static final long serialVersionUID = 7907228496247515732L;

            @Override
            public void menuSelected(MenuItem selectedItem)
            {
                _mainPanel.removeAllComponents();
                _mainPanel.addComponent(new AddEditAOItem());
            }
        });
        addToStorageMenu.addItem("Обменный курс", new Command()
        {
            private static final long serialVersionUID = 7907228496247515732L;

            @Override
            public void menuSelected(MenuItem selectedItem)
            {
                _mainPanel.removeAllComponents();
                _mainPanel.addComponent(new AddEditAOItemExchange());
            }
        });
        storageMenu.addItem("Сохранить", new Command()
        {
			private static final long serialVersionUID = -4548931049514749699L;

			@Override
            public void menuSelected(MenuItem selectedItem)
            {
                _mainPanel.removeAllComponents();
                AOData.getInstance().doSave();
            }
        });
        return menu;
    }

    public static synchronized Panel createMainPanel()
    {
        return _mainPanel;
    }

    public static void refillSelect(AbstractSelect select, List<? extends AOItemBase> list)
    {
        select.removeAllItems();
        for (AOItemBase item : list)
        {
            select.addItem(item.getCode());
            select.setItemCaption(item.getCode(), item.getTitle());
        }
    }
}
